# 列表 一、取值-》 1. index 索引取单个值 2.slice 切片取一些值（取完后仍为列表）
# names = "Cathy,Keshi,Yangyang"  # 01245...
# names_list = ["Cathy","Keshi","Yangyang"]
# 0 ——1—— 2
#
# print(names[0:5])# 0表示第0个，5表示五个字母
# print(names_list[0])
#
#
# Python中一切皆是对象！
# 二、列表增加
# 2.1append() 增加元素到list的末尾
#    append（） 一般用来新建列表会使用到
names_C = []
names = ["Cathy","Keshi","Yangyang","Pipi"]
stu_score = [99,55,66,99,100,25,56,77,88]
names.append("Cathy")
names.append("Youge")
for i in names:
    if i[0] == "C":# 英文名开头首字母为C
        names_C.append(i)
print("names_C=",names)
print(names)

# 2.2 insert() 插入元素
names.insert(2,"Meinv")
names.insert(5,"Cathy")
print("插入meinv之后的names:",names)

# 3.删除/移除
# 多行注释快捷键ctrl+/
# 3.1remove() 使用remove之前，应该先判断元素是否存在list中
# print("Cathy" in names)
# name_user = input("删除内容：")
# while True:
#     if name_user in names:
#         names.remove(name_user)
#     else:
#         break
# print("remove之后的names:",names)
# 3.2pop() 删除 pop()不填内容==》append()删除末尾
# names.pop()
# print(names)

# 4.index()
print(names.index("Cathy"))

# 5.sort()排序 默认从小到大排序 reverse = False(默认) 小-》大
stu_score.sort(reverse=True)# 从大-》小 True
print(stu_score)
names.sort()# 字符串 以首字母按照ASCII编码表顺序排序
print(names)

# 3.count() 查看某元素的次数
print(stu_score.count(99))

# 7.clear 清空list
# names.clear()
# print(names)

# 8.extend() 是有顺序的 操作哪一个列表，那一个列表就先写
names.extend(names_C)
print(names)
# 列表是真正意义的复制
# 1. 假复制 用= 进行赋值  !!!错误的列表复制操作
# names = ["Cathy","Yang","cyt","gyl"]
# names_yuan = names
# names.append("Tsghjd")
# print("names=",names)
# print("names_yuan",names_yuan)


# 2. 正确的方法2.1copy()

# names = ["Cathy","Yang","cyt","gyl"]
# names_yuan = names.copy()
# names.append("Tsghjd")
# print("names=",names)
# print("names_yuan",names_yuan)
# 2.2 切片[:]

names = ["Cathy","Yang","cyt","gyl"]
names_yuan = names[:]
names.append("Tsghjd")
print("names=",names)
print("names_yuan",names_yuan)

# 枚举--列表的高级用法
stu_score = [99,55,66,99,100,25,56,77,88]
# 目标： 使用pop删除 成绩为99的所有值
# 本质： list 本身有 index and value
for index,value in enumerate(stu_score):
    # print(index,value)
    if value == 99:
        # stu_score.pop(index)
        stu_score[index] += 1
print(stu_score)


# 课本项目
#  P67
phrase = "Don't panic!"
plist = list(phrase) # str 转换 list 使用的方法是 list(str)
print(phrase)
print(plist)

for i in range(4):
    # 循环执行4次 删除末尾 使用方法是pop()
    plist.pop()
print(plist)
plist.pop(0)
print(plist)
plist.remove("'")
print(plist)
# extend ==>plist + [plist.pop(),plist.pop()]
plist.extend([plist.pop(),plist.pop()])# 运算顺序
#    ["o","n",",t"]               a           p
print(plist)
plist.insert(2,plist.pop(3))
new_phrase = ''.join(plist)# 列表list 转换 str
print("plist=",plist)# 打乱列表顺序
print("new_phrase",new_phrase)

# panic2 列表切片是非破坏性的
phrase = "Don't panic!"
plist = list(phrase)
print(phrase)
print(plist)

new_phrase = ''.join(plist[1:3])
new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])
print(plist)
print(new_phrase)
# for 循环了解列表
# 列表的字符串分行打印前面有一个制表符（这是\t的作用）
paranoid_android = "Marvin"
letters = list(paranoid_android)
for char in letters:
    print('\t',char)

# for 循环了解切片
paranoid_android = "Marvin"
letters = list(paranoid_android)
for char in letters[:6]:
    print('\t',char)
print()
for char in letters[-7:]:
    print('\t'*2,char)# '*2'表示在打印对象前面插入两个制表符
print()
for char in letters[12:20]:
    print('\t'*3,char)

# vowies4
vowels = ['a', 'e', 'i', 'o', 'u']
word = input("Provid a word to search for vowels:")

found = {}
found['a'] = 0
found['e'] = 0
found['i'] = 0
found['o'] = 0
found['e'] = 0
for letter in word:
    if letter in vowels:
        found[letter] += 1

for k, v in sorted(found.items()):
    print(k, 'was found', v, 'time(s).')